package com.lxg.controller.adminController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxg.pojo.*;
import com.lxg.service.adminService.AdminAdminService;
import com.lxg.service.adminService.AdminBooksService;
import com.lxg.service.adminService.AdminProblemService;
import com.lxg.service.adminService.AdminUserService;
import com.lxg.service.booksService.BooksShowService;
import com.lxg.service.historyService.HistoryShowService;
import com.lxg.service.userService.UserBorrowService;
import com.lxg.service.userService.UserProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.regex.Pattern;

@Controller
public class AdminJump {

   @Autowired
    AdminBooksService adminBooksService;
   @Autowired
    AdminUserService adminUserService;
   @Autowired
    AdminAdminService adminAdminService;

   @Autowired
    AdminProblemService adminProblemService;
    @Autowired
    BooksShowService booksShowService;

    @Autowired
    UserProblemService userProblemService;

    @Autowired
    UserBorrowService userBorrowService;

    @Autowired
    HistoryShowService historyShowService;

    @RequestMapping("/admin/music")
    public String tomusic(){
        return "admin/adminMusic";
    }

    //回主页
    @GetMapping("/admin/home")
    public String toadminhome(HttpServletRequest req, HttpSession session){
        //获取全部图书数量（每一种算一本）
        Integer typeNumber = adminBooksService.getTypeNumber();
        //获取所有图书库存数量
        Integer allNum = adminBooksService.getAllNum();
        //获取已借出数量
        Integer borrowNum = adminBooksService.getBorrowNum();
        //获取普通用户用户数量
        Integer usersNum = adminUserService.getUsersNum();
        //获取管理员数量
        Integer amdinNum = adminAdminService.getAmdinNum();
        //获取超级管理员数量
        Integer superAdminNum = adminAdminService.getSuperAdminNum();

        int AllUsersNum = amdinNum + usersNum + superAdminNum;

        req.setAttribute("booksTypeNum",typeNumber);
        req.setAttribute("allNum",allNum);
        req.setAttribute("borrowNum",borrowNum);
        req.setAttribute("usersNum",usersNum);
        req.setAttribute("superAdminNum",superAdminNum);
        req.setAttribute("adminNum",amdinNum);
        req.setAttribute("AllUsersNum", AllUsersNum);
        return "admin/admin";
    }
    //用户管理
    @GetMapping("/admin/userManage")
    public String toauserManage(Model model,HttpSession session,HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){

        String addResult = request.getParameter("addResult");
        System.out.println(addResult);

        if(addResult!=null){

            if(addResult.equals("false") ){

                request.setAttribute("addMsg","信息不能为空！");
            }else if(addResult.equals("true")){
                request.setAttribute("addMsg","添加成功！！");
            }
        }


        String updateResult = request.getParameter("updateResult");

        if(updateResult!=null){

            if(updateResult.equals("false") ){

                request.setAttribute("updateMsg","信息不能为空！");
            }else if(updateResult.equals("true")){
                request.setAttribute("updateMsg","修改成功！！");
            }
        }

        String searchUserText = request.getParameter("searchUserText");
        PageInfo<User> pageInfo = null;
        if (searchUserText=="" || searchUserText==null){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<User> allUsers = adminUserService.getAllUser();
            pageInfo = new PageInfo<>(allUsers,3);
            model.addAttribute("pageInfo",pageInfo);
            return "admin/userManage";
        }else{
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<User> searchNameBooks = adminUserService.getAllLikeUserByName(searchUserText);
            ArrayList<User> searchUsers = searchNameBooks;
            if (searchNameBooks.size()==0){
                System.out.println("test");
                PageHelper.startPage(pageNum,pageSize);
                ArrayList<User> searchUserNameBooks = adminUserService.getAllLikeUserByUserName(searchUserText);
                searchUsers = searchUserNameBooks;
            }
            pageInfo = new PageInfo<>(searchUsers,3);
            model.addAttribute("pageInfo",pageInfo);
            return "admin/userManage";
        }



    }

    //图书管理
    @GetMapping("/admin/bookManage")
    public String toBookManage(Model model,HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){


        String addResult = request.getParameter("addResult");

        if(addResult!=null){

            if(addResult.equals("false") ){

                request.setAttribute("addMsg","信息不能为空！");
            }else if(addResult.equals("true")){
                request.setAttribute("addMsg","添加成功！！");
            }
        }


        String updateResult = request.getParameter("updateResult");

        if(updateResult!=null){

            if(updateResult.equals("false") ){

                request.setAttribute("updateMsg","信息不能为空！");
            }else if(updateResult.equals("true")){
                request.setAttribute("updateMsg","修改成功！！");
            }
        }




        String searchBookText = request.getParameter("searchBookText");

        PageInfo<Books> pageInfo = null;

        if (searchBookText=="" || searchBookText==null){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> allBooks = booksShowService.getAllBooks();
            pageInfo = new PageInfo<>(allBooks,3);
            System.out.println(pageInfo.getList().size());
            model.addAttribute("pageInfo",pageInfo);

//            request.setAttribute("searchText","");

            return "/admin/bookManage";
        }else{

            System.out.println("test:"+searchBookText);

            request.setAttribute("searchText",searchBookText);

            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Books> searchNameBooks = booksShowService.getAllLikeBooksByName(searchBookText);
            ArrayList<Books> searchBooks = searchNameBooks;

            if (searchNameBooks.size()==0){
                PageHelper.startPage(pageNum,pageSize);
                ArrayList<Books> searchAuthorBooks = booksShowService.getAllLikeBooksByAuthor(searchBookText);
                searchBooks = searchAuthorBooks;
            }


            pageInfo = new PageInfo<>(searchBooks,3);
            System.out.println(pageInfo.getList().size());
            model.addAttribute("pageInfo",pageInfo);
            return "/admin/bookManage";
        }

    }



    //审批管理
    @GetMapping("/admin/approval")
    public String toApproval(Model model,HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){


        String agreeResult = request.getParameter("agreeResult");
        if (agreeResult!=null){
            if( agreeResult.equals("true") || agreeResult.equals("false")){
                request.setAttribute("agreeResult",agreeResult);
            }
        }

        String deleteResult = request.getParameter("deleteResult");
        if (deleteResult!=null){
            if( deleteResult.equals("true") || deleteResult.equals("false")){
                request.setAttribute("deleteResult",deleteResult);
            }
        }

        String result = request.getParameter("result");
        if (result!=null){
            if( result.equals("true") || result.equals("false")){
                request.setAttribute("result",result);
            }
        }


        String searchHistoryText = request.getParameter("searchHistoryText");
        PageInfo<History> pageInfo = null;
        String status = request.getParameter("status");
        if(status !=null && status.equals("true")){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<History> allHistories = historyShowService.getAllHistoryByStatus ();
            pageInfo = new PageInfo<>(allHistories,3);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("status",status);
            return "admin/approval";
        }


        if (searchHistoryText=="" || searchHistoryText==null){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<History> allProbelms = historyShowService.getAllHistory();
            pageInfo = new PageInfo<>(allProbelms,3);
            model.addAttribute("pageInfo",pageInfo);
            return "/admin/approval";
        }else {
            request.setAttribute("searchText", searchHistoryText);
            PageHelper.startPage(pageNum, pageSize);
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            boolean matches = pattern.matcher(searchHistoryText).matches();
            ArrayList<History> searchUidHistorys = new ArrayList<>();
            ArrayList<History> searchHistorys = new ArrayList<>();
            if(matches==true){
                searchUidHistorys = historyShowService.getAllHistoryByUid(Integer.valueOf(searchHistoryText));
                searchHistorys = searchUidHistorys;
            }else{
                ArrayList<History> searchNameHistroys = new ArrayList<>();
                if (searchUidHistorys.size() == 0) {
                    PageHelper.startPage(pageNum, pageSize);
                    searchNameHistroys = historyShowService.getAllHistoryByLikeBookName(searchHistoryText);
                    searchHistorys = searchNameHistroys;
                }
            }
            pageInfo = new PageInfo<>(searchHistorys, 3);
            model.addAttribute("pageInfo", pageInfo);
            return "/admin/approval";
        }




    }

    //管理员管理
    @GetMapping("/admin/adminManage")
    public String toAdminManage(Model model,HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        String addResult = request.getParameter("addResult");

        if(addResult!=null){

            if(addResult.equals("false") ){

                request.setAttribute("addMsg","信息不能为空！");
            }else if(addResult.equals("true")){
                request.setAttribute("addMsg","添加成功！！");
            }
        }


        String updateResult = request.getParameter("updateResult");

        if(updateResult!=null){

            if(updateResult.equals("false") ){

                request.setAttribute("updateMsg","信息不能为空！");
            }else if(updateResult.equals("true")){
                request.setAttribute("updateMsg","修改成功！！");
            }
        }

        String searchAdminText = request.getParameter("searchAdminText");
        PageInfo<Admin> pageInfo = null;
        if (searchAdminText=="" || searchAdminText==null){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Admin> allUsers = adminAdminService.getAllAdmin();
            pageInfo = new PageInfo<>(allUsers,3);
            model.addAttribute("pageInfo",pageInfo);
            return "admin/adminManage";
        }else{
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Admin> searchAdmin = adminAdminService.getAllLikeAdminByUserName(searchAdminText);
            pageInfo = new PageInfo<>(searchAdmin,3);
            model.addAttribute("pageInfo",pageInfo);
            return "admin/adminManage";
        }

    }


    //反馈管理
    @GetMapping("/admin/problemManage")
    public String toProblemManage(Model model,HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){




        String searchProblemText = request.getParameter("searchProblemText");

        PageInfo<Problem> pageInfo = null;


        String status = request.getParameter("status");
        if(status !=null && status.equals("true")){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Problem> allUnsolvedProbelms =adminProblemService.getAllUnsolved ();
            pageInfo = new PageInfo<>(allUnsolvedProbelms,3);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("status",status);
            return "/admin/problemManage";
        }


        if (searchProblemText=="" || searchProblemText==null){
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Problem> allProbelms = userProblemService.getAllProblems();
            pageInfo = new PageInfo<>(allProbelms,3);
            model.addAttribute("pageInfo",pageInfo);

            return "/admin/problemManage";
        }else {

            request.setAttribute("searchText", searchProblemText);

            PageHelper.startPage(pageNum, pageSize);
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            boolean matches = pattern.matcher(searchProblemText).matches();
            ArrayList<Problem> searchUidProblems = new ArrayList<>();
            ArrayList<Problem> searchProblems = new ArrayList<>();
            if(matches==true){
                 searchUidProblems = userProblemService.getProblemsByUid(Integer.valueOf(searchProblemText));
                 searchProblems = searchUidProblems;
            }else{
                System.out.println("testPro1");
                ArrayList<Problem> searchNameProblems = new ArrayList<>();
                if (searchUidProblems.size() == 0) {
                    System.out.println("testPro2");
                    PageHelper.startPage(pageNum, pageSize);
                    searchNameProblems = userProblemService.getProblemsByLikeName(searchProblemText);
                    searchProblems = searchNameProblems;
                    System.out.println(searchNameProblems.size());
                }
                if (searchNameProblems.size() == 0) {
                    System.out.println("testPro3");
                    PageHelper.startPage(pageNum, pageSize);
                    ArrayList<Problem> searchBodyProblems = userProblemService.getProblemsByLikeBody(searchProblemText);
                    searchProblems = searchBodyProblems;
                }
            }
            System.out.println("testPro4");
            pageInfo = new PageInfo<>(searchProblems, 3);
            model.addAttribute("pageInfo", pageInfo);
            return "/admin/problemManage";
        }
    }
}
